import React from 'react'
import {
  Form,
  Input,
  Button,
  Typography,
  Image,
  message
} from 'antd'
import {
  UserOutlined,
  LockOutlined,
  EyeInvisibleOutlined,
  EyeTwoTone,
} from '@ant-design/icons'
import { useNavigate } from 'react-router-dom'
import { authUtils } from '@/components/AuthGuard'
const { Title, Text } = Typography

interface LoginForm {
  username: string
  password: string
}

// 默认登录信息
const DEFAULT_LOGIN: LoginForm = {
  username: 'admin',
  password: '123456'
}

const Login: React.FC = () => {
  const [form] = Form.useForm()
  const navigate = useNavigate()
  const [loading, setLoading] = React.useState(false)

  const onFinish = async (values: LoginForm) => {
    console.log('登录信息:', values)
    setLoading(true)
    
    try {
      // 模拟登录验证
      if (values.username === 'admin' && values.password === '123456') {
        // 设置登录状态
        authUtils.login('mock-token-' + Date.now())
        message.success('登录成功！')
        // 延迟跳转，让用户看到成功提示
        setTimeout(() => {
          navigate('/home')
        }, 1000)
      } else {
        message.error('用户名或密码错误！')
      }
    } catch (error) {
      message.error('登录失败，请稍后重试！')
    } finally {
      setLoading(false)
    }
  }

  return (
    <div className='h-[80vh] flex '>
      {/* 左侧展示区域 */}
      <div className='w-[700px] flex items-center justify-center'>
        <Image
        preview={false}
          width={500}
          height={500}
          src='https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
        />
      </div>

      {/* 右侧登录区域 */}
      <div className='w-96 bg-slate-800 flex items-center justify-center p-8'>
        <div className='w-full max-w-sm'>
          {/* 登录头部 */}
          <div className='text-center mb-8'>
            <Title
              level={2}
              className='!text-white !mb-2 flex items-center justify-center gap-2'>
              欢迎回来
              <span className='text-2xl'>👋</span>
            </Title>
          </div>

          {/* 登录表单 */}
          <Form
            form={form}
            name='login'
            onFinish={onFinish}
            autoComplete='off'
            size='large'
            className='w-full'
            initialValues={DEFAULT_LOGIN}>
            {/* 用户名输入 */}
            <Form.Item
              name='username'
              rules={[
                { required: true, message: '请输入用户名!' },
                { min: 3, message: '用户名至少3个字符!' }
              ]}
              className='mb-6'>
              <Input
                prefix={<UserOutlined className='text-slate-400' />}
                placeholder='账号'
                className='bg-slate-700 border-slate-600 text-white placeholder:text-slate-400 hover:border-blue-500 focus:border-blue-500'
              />
            </Form.Item>

            {/* 密码输入 */}
            <Form.Item
              name='password'
              rules={[
                { required: true, message: '请输入密码!' },
                { min: 6, message: '密码至少6个字符!' }
              ]}
              className='mb-6'>
              <Input.Password
                prefix={<LockOutlined className='text-slate-400' />}
                placeholder='密码'
                className='bg-slate-700 border-slate-600 text-white placeholder:text-slate-400 hover:border-blue-500 focus:border-blue-500'
                iconRender={(visible) =>
                  visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />
                }
              />
            </Form.Item>

            {/* 登录按钮 */}
            <Form.Item className='mb-6'>
              <Button
                type='primary'
                htmlType='submit'
                loading={loading}
                className='w-full h-12 text-lg font-medium bg-blue-600 hover:bg-blue-700 border-0 rounded-lg'>
                {loading ? '登录中...' : '登录'}
              </Button>
            </Form.Item>
          </Form>
          {/* 版权信息 */}
          <div className='text-center mt-8'>
            <Text className='text-slate-500 text-xs'>Copyright © 2024</Text>
          </div>
        </div>
      </div>
    </div>
  )
}

export default Login
